#include "XgW32ProcessModules.h"
#include <tchar.h>

XgW32ProcessModules::XgW32ProcessModules(DWORD pid)
: _pid(pid)
{
	refresh();
}

XgW32ProcessModules::XgW32ProcessModules(const XgW32ProcessModules& modules)
: _pid(modules._pid)
{
	refresh();
}

XgW32ProcessModules::~XgW32ProcessModules(void)
{
	clear();
}

bool XgW32ProcessModules::refresh()
{
	HMODULE aModules[1024] = { 0 };
	
	clear();

	XgW32Process proc(_pid);

	DWORD cModules = proc.enumModules(aModules, sizeof(aModules));

	if(!cModules)
	{
		OutputDebugString( _T("XgW32ProcessModules::refresh : No modules were found in process ") );
		OutputDebugString( proc.name().c_str() );
		OutputDebugString( _T("\n") );
		return false;
	}

	for( UINT i = 0; i < cModules; i++ )
	{
		HMODULE hModule = aModules[i];
		_map[hModule] = XgW32ProcessModule(hModule, proc);
	}
	return true;
}

bool XgW32ProcessModules::clear()
{
	_map.clear();
	return true;
}

XgW32ProcessModules::Iterator XgW32ProcessModules::begin()
{
	return _map.begin();
}

XgW32ProcessModules::Iterator XgW32ProcessModules::end()
{
	return _map.end();
}